import os
import json
import tkinter as tk
from tkinter import filedialog, messagebox
from tools import save_to_config, read_from_config, validate_ip_address, create_input_with_button, validate_ip_address

def show_settings(right_frame, log_operation, log_text):
    for widget in right_frame.winfo_children():  # 清除右边的内容
        widget.destroy()

    right_frame.config(bg="white")

    # 读取已有的配置
    initial_password_folder_path = read_from_config("password_folder_path")
    initial_telnet_path = read_from_config("open_telnet_adress")
    initial_ip_address = read_from_config("ip_address")
    initial_router_settings_url = read_from_config("router_settings_url")  # 新增读取路由器地址
    initial_password_row = read_from_config("password_row")

    # 创建标题标签
    title_label = tk.Label(right_frame, text="设置页面", font=("Arial", 20), bg="white")
    title_label.pack(pady=10)

    # "密码文件位置" 标签和输入框
    password_folder_label = tk.Label(right_frame, text="密码文件夹位置", font=("Arial", 20), bg="white")
    password_folder_label.place(x=100, y=50, width=500, height=30)

    password_folder_entry = tk.Entry(right_frame, font=("Arial", 20), width=50, borderwidth=2, relief="solid")
    password_folder_entry.place(x=100, y=80, width=500, height=30)
    password_folder_entry.insert(0, initial_password_folder_path)

    def open_password_folder_folder():
        folder_path = filedialog.askdirectory()
        if folder_path:
            password_folder_entry.delete(0, tk.END)
            password_folder_entry.insert(0, folder_path)
            save_to_config("password_folder_path", folder_path, log_text)


    open_button = tk.Button(right_frame, text="打开", font=("Arial", 15), command=open_password_folder_folder)
    open_button.place(x=610, y=80, width=80, height=30)

    # 密码列列名label，输入框，保存按钮
    create_input_with_button(right_frame=right_frame, label_text="密码列列名", initial_insert_arg = initial_password_row, y_position=130, log_text=log_text, config_key="password_row")

    # "开 telnet 文件地址" 标签和输入框
    telnet_label = tk.Label(right_frame, text="开telnet文件地址", font=("Arial", 15), bg="white")
    telnet_label.place(x=100, y=210, width=500, height=30)

    telnet_entry = tk.Entry(right_frame, font=("Arial", 15), width=50, borderwidth=2, relief="solid")
    telnet_entry.place(x=100, y=240, width=500, height=30)
    telnet_entry.insert(0, initial_telnet_path)

    def open_telnet_file():
        file_path = filedialog.askopenfilename()
        if file_path:
            telnet_entry.delete(0, tk.END)
            telnet_entry.insert(0, file_path)
            save_to_config("open_telnet_adress", file_path, log_text)

    open_telnet_button = tk.Button(right_frame, text="打开", font=("Arial", 15), command=open_telnet_file)
    open_telnet_button.place(x=610, y=240, width=80, height=30)

    # ip地址label，输入框，保存按钮
    create_input_with_button(right_frame=right_frame, label_text="IP地址", initial_insert_arg = initial_ip_address, y_position=290, log_text=log_text, config_key="ip_address", validate_func=validate_ip_address)

    # 路由器地址地址label，输入框，保存按钮
    create_input_with_button(right_frame=right_frame, label_text="路由器设置页地址", initial_insert_arg = initial_router_settings_url, y_position=370, log_text=log_text, config_key="router_settings_url", validate_func=validate_ip_address)


    